package 行为型模式.责任链;

/**
 * @ClassNameChainOfResponsibilityTest
 * @Description
 * @Author
 * @Date2022/1/5 15:46
 **/
public class ChainOfResponsibilityTest {
    public static void main(String[] args) {
        Leader leader1 = new ClassAdviser();
        Leader leader2 = new DepartmentHead();
        Leader leader3 = new Dean();
        leader1.setNext(leader2);
        leader2.setNext(leader3);

        // 不同天数请假验证
        for (int i = 0; i < 11; i++) {
            leader1.handleRequest(i + 1);
        }
    }
}

// 领导类
abstract class Leader {
    private Leader next;

    public Leader getNext() {
        return next;
    }

    public void setNext(Leader next) {
        this.next = next;
    }

    //处理请求的方法
    public abstract void handleRequest(int days);
}

// 班主任
class ClassAdviser extends Leader {

    @Override
    public void handleRequest(int days) {
        if (days <= 2) {
            System.out.println("班主任批准假期" + days + "天");
        } else {
            if (getNext() == null) {
                System.out.println("请假天数太长，不批准");
            } else {
                getNext().handleRequest(days);
            }
        }
    }
}

// 系主任
class DepartmentHead extends Leader {

    @Override
    public void handleRequest(int days) {
        if (days <= 7) {
            System.out.println("系主任批准假期" + days + "天");
        } else {
            if (getNext() == null) {
                System.out.println("请假天数太长，不批准");
            } else {
                getNext().handleRequest(days);
            }
        }
    }
}

// 系主任
class Dean extends Leader {

    @Override
    public void handleRequest(int days) {
        if (days <= 10) {
            System.out.println("院长批准假期" + days + "天");
        } else {
            if (getNext() == null) {
                System.out.println("请假天数太长，不批准");
            } else {
                getNext().handleRequest(days);
            }
        }
    }
}
